﻿///***************************************************************
/// 시  스  템 : Updms.Crd
/// 단위시스템 : CRD
/// 계      층 : Presentation Layer
/// 프로그램명 : Crd2017r
/// 파  일  명 : Crd2017r.aspx.cs
/// 설      명 : 월별카드사용내역 - 달력형태
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2008-11-18 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;

using Base.Dac;
using Updms.Crd.Exe;


namespace Updms.Crd
{
	public partial class Crd2017r : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Crd2017r";
		public static readonly string gs_path_info = "/updms/Crd/" + PID + ".aspx";

		Global global = new Global();
		CommCode.Crd crd = new CommCode.Crd(LANG_SET);

		//검색
		protected string kw1 = "";	//카드코드
		protected string kw2 = "";	//기준년월
		protected string kw3 = "";	//계정
		protected string kw4 = "";	//조회타입

		//메뉴 선택값
		protected string gs_sm = "";

		// 페이지모드
		private string gs_page_mode = "";

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetCardInfo();
			SetBaseYear(ddl_search_card_cd.SelectedValue);
			SetAccount();
			SetShowType();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion	

		#region SetInit, SetSearchParameter

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";

			gs_sm = (Request.QueryString["sm"] == null || Request.QueryString["sm"].Trim().Length != 7)
					? global.GetDirDepth(PID) : Request.QueryString["sm"].ToString();
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = ddl_search_card_cd.SelectedValue;
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						ddl_search_card_cd.SelectedValue = kw1;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
					{
						kw2 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
						ddl_search_base_year.SelectedValue = kw2.Substring(0, 4).ToString();
						ddl_search_base_month.SelectedValue = kw2.Substring(5, 2).ToString();
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = ddl_search_account.SelectedValue;
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						ddl_search_account.SelectedValue = kw3;
					}
					
					if (String.IsNullOrEmpty(Request.QueryString["kw4"]))	// url에 값이 없으면
					{
						kw4 = ddl_search_show_type.SelectedValue;
					}
					else
					{
						kw4 = Server.UrlDecode(Request.QueryString["kw4"]);
						ddl_search_show_type.SelectedValue = kw4;
					}
					
					break;
				case "SEARCH":
					kw1 = ddl_search_card_cd.SelectedValue;
					kw2 = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
					kw3 = ddl_search_account.SelectedValue;
					kw4 = ddl_search_show_type.SelectedValue;
					break;
			}
		}


		#endregion

		#region 마스터 Method (SetCardInfo, SetBaseYear, SetAccount)

		//카드정보
		private void SetCardInfo()
		{
			DataSet lo_ds = new DataSet();
			Crd1010m_NT _Crd1010m_NT = new Crd1010m_NT();

			lo_ds.Merge(_Crd1010m_NT.GetCardInfoList("A"));

			ddl_search_card_cd.Items.Clear();
			ddl_search_card_cd.DataSource = lo_ds;
			ddl_search_card_cd.DataMember = "UPDMS_CRD_INFO";
			ddl_search_card_cd.DataTextField = "Card_Nm_Yn";
			ddl_search_card_cd.DataValueField = "Card_Cd";
			ddl_search_card_cd.DataBind();
		}

		//기준년도 설정
		private void SetBaseYear(string ls_card_cd)
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();
			ddl_search_base_year.Items.Clear();

			lo_dp.AddProperty("ls_card_cd", typeof(string), ls_card_cd);

			using (Crd1011m_NT _Crd1011m_NT = new Crd1011m_NT())
			{
				lo_ds.Merge(_Crd1011m_NT.GetMinNMaxBizDt(lo_dp));
			}

			if (DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Min_Dt_Y") > 0)
			{
				int li_min_dt_y = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Min_Dt_Y");
				int li_max_dt_y = DataSetUtil.GetFldInt(lo_ds, "UPDMS_CRD_PAYMNT_DT", "Max_Dt_Y");

				for (int i = li_min_dt_y; i <= li_max_dt_y; i++)
				{
					string ls_year = i.ToString();
					ddl_search_base_year.Items.Add(new ListItem(ls_year, ls_year));
				}

				//기본값으로 현재년도 선택
				if (WebControlUtil.CheckDdlValue(ddl_search_base_year, DateUtil.GetYearEx(0, "yyyy")))
				{
					ddl_search_base_year.SelectedValue = DateUtil.GetYearEx(0, "yyyy"); // 기본값은 현재년도
				}
				else
				{
					ddl_search_base_year.SelectedValue = li_max_dt_y.ToString();
				}

				//기본값으로 현재월 선택
				ddl_search_base_month.SelectedValue = DateUtil.GetMonthEx(0, "MM");
			}
			else
			{
				ddl_search_base_year.Items.Add(new ListItem(DateUtil.GetYearEx(0, "yyyy"), DateUtil.GetYearEx(0, "yyyy")));
				CallMsgBoxInAjax("MSG_ERR_142");	//지정된 결제일이 없습니다.\n'카드결제일관리'에서 추가해 주세요
			}
		}

		//계정
		private void SetAccount()
		{
			crd.SetAccount(ddl_search_account);

			ddl_search_account.Items.Insert(0, new ListItem(DDL_SELECT, ""));
		}

		//조회타입
		private void SetShowType()
		{
			crd.SetShowType(ddl_search_show_type);
		}

		//카드변경시
		protected void ddl_search_card_cd_SelectedIndexChanged(object sender, EventArgs e)
		{
			SetBaseYear(ddl_search_card_cd.SelectedValue);
		}

		#endregion

		#region 디테일 Method (SetPrevNextMonth, SetEnabledMonthBtn, SetThisMonth)

		//이전달, 다음달버튼 누르면
		private void SetPrevNextMonth(int pi_month_cnt)
		{
			string ls_ym = ddl_search_base_year.SelectedValue + "-" + ddl_search_base_month.SelectedValue;
			string ls_calc_ym = DateUtil.GetCalcMonth(ls_ym, pi_month_cnt, "yyyy-MM");

            if (WebControlUtil.CheckDdlValue(ddl_search_base_year, ls_calc_ym.Substring(0, 4).ToString()))
			{
				ddl_search_base_year.SelectedValue = ls_calc_ym.Substring(0, 4).ToString();
				ddl_search_base_month.SelectedValue = ls_calc_ym.Substring(5, 2).ToString();
			}
			
			//버튼처리
			SetEnabledMonthBtn();
		}
		
		//이전달, 다음달버튼 처리
		private void SetEnabledMonthBtn()
		{
			ibtn_prev_month.Enabled 
				= (ddl_search_base_year.SelectedIndex == 0 && ddl_search_base_month.SelectedIndex == 0) ? false : true;
			
			ibtn_next_month.Enabled 
				= (ddl_search_base_year.SelectedIndex == ddl_search_base_year.Items.Count - 1 && ddl_search_base_month.SelectedIndex == 11) ? false : true;
		}
		
		//당월설정
		private void SetThisMonth()
		{
			string ls_this_ym = DateUtil.GetNow("yyyy-MM");
			ddl_search_base_year.SelectedValue = ls_this_ym.Substring(0, 4).ToString();
			ddl_search_base_month.SelectedValue = ls_this_ym.Substring(5, 2).ToString();
			SetEnabledMonthBtn();
		}

		#endregion

		#region 리스트 (SelectProcess, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_ym", typeof(string), kw2);
			lo_dp.AddProperty("ls_account", typeof(string), kw3);
			lo_dp.AddProperty("ls_show_type", typeof(string), kw4);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);
			
			using (Crd2017r_NT _Crd2017r_NT = new Crd2017r_NT())
			{
				lo_ds.Merge(_Crd2017r_NT.GetMonthlyUsingList(lo_dp));

				GridView1.DataSource = lo_ds;
				GridView1.DataBind();
			}
			lbl_this_month.Text = DateUtil.ConvertDateMask(kw2, "yyyy-MM", LANG_SET);
		}

		//검색
		private void SearchProcess()
		{
			gs_page_mode = "R";
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();

			SelectProcess();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				using (AccessCheck access = new AccessCheck())
				{
					//접근권한 체크
					if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
					{
						Response.Redirect(URL_NO_AUTHORITY_REDIRECT_BASE);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
					}
				}
			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			switch (gs_page_mode)
			{
				case "":	// 처음 페이지로드시
					table_list_set.Visible = false;
					break;
				case "R":	// 검색버튼 클릭시
					table_list_set.Visible = true;
					break;
			}
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SetEnabledMonthBtn();
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_card_cd", typeof(string), kw1);
			lo_dp.AddProperty("ls_base_ym", typeof(string), kw2);
			lo_dp.AddProperty("ls_account", typeof(string), kw3);
			lo_dp.AddProperty("ls_show_type", typeof(string), kw4);
			lo_dp.AddProperty("ls_lang_set", typeof(string), LANG_SET);

			using (Crd2017r_NT _Crd2017r_NT = new Crd2017r_NT())
			{
				lo_ds.Merge(_Crd2017r_NT.GetMonthlyUsingList(lo_dp));
			}
			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}
		
		//이전달
		protected void ibtn_prev_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(-1);
			SearchProcess();
		}
		
		//다음달
		protected void ibtn_next_month_Click(object sender, EventArgs e)
		{
			SetPrevNextMonth(1);
			SearchProcess();
		}

		//당월
		protected void ibtn_this_month_Click(object sender, EventArgs e)
		{
			SetThisMonth();
			SearchProcess();
		}
		
		#endregion

	}
}
